#include "my_svpwm.hpp"
void my_svpwm::recalculate_sin_table_variable(int max_time_for_single_phase)
{
    double division_of_time = max_time_for_single_phase;
    division_of_time = 1 / division_of_time;
    this->max_time_for_single_phase = max_time_for_single_phase;
    for (int i = 0; i < this->max_time_for_single_phase; i++)
    {
        this->sin_table_variable[i] = this->half_max_duty * sin(2 * 3.14159265 * division_of_time) + this->half_max_duty;
    }
}
void my_svpwm::change_max_duty(int max_duty)
{
    this->max_duty = max_duty;
}
void my_svpwm::svpwm_run(void)
{
    TIM1->CCR1 = this->sin_table_variable[this->time_base_for_single_phase[0]];
    TIM1->CCR2 = this->sin_table_variable[this->time_base_for_single_phase[1]];
    TIM1->CCR3 = this->sin_table_variable[this->time_base_for_single_phase[2]];
    this->time_base_for_single_phase[0]++;
    this->time_base_for_single_phase[1]++;
    this->time_base_for_single_phase[2]++;
    if (this->time_base_for_single_phase[0] >= this->max_time_for_single_phase)
    {
        this->time_base_for_single_phase[0] = 0;
    }
    if (this->time_base_for_single_phase[1] >= this->max_time_for_single_phase)
    {
        this->time_base_for_single_phase[1] = 0;
    }
    if (this->time_base_for_single_phase[2] >= this->max_time_for_single_phase)
    {
        this->time_base_for_single_phase[2] = 0;
    }
}
void my_svpwm::set_phase_difference_180(void)
{
    //居中代码
    if (((this->max_time_for_single_phase >> 1) << 1) = this->max_time_for_single_phase)
    {
        this->time_base_for_single_phase[1] = this->time_base_for_single_phase[0] + this->max_time_for_single_phase / 2;
    }
    else
    {
        this->time_base_for_single_phase[1] = this->time_base_for_single_phase[0] + this->max_time_for_single_phase / 2 + 1;
    }
    if (time_base_for_single_phase[1] >= this->max_time_for_single_phase)
    {
        time_base_for_single_phase[1] -= this->max_time_for_single_phase;
    }
}
void my_svpwm::set_phase_difference_120(void)
{
    this->time_base_for_single_phase[1] = this->time_base_for_single_phase[0] + this->max_time_for_single_phase / 3;
    this->time_base_for_single_phase[2] = this->time_base_for_single_phase[0] + this->max_time_for_single_phase * 2 / 3;
    if (time_base_for_single_phase[1] >= this->max_time_for_single_phase)
    {
        time_base_for_single_phase[1] -= this->max_time_for_single_phase;
    }
    if (time_base_for_single_phase[2] >= this->max_time_for_single_phase)
    {
        time_base_for_single_phase[2] -= this->max_time_for_single_phase;
    }
}